﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Collections;

namespace Smartmanufact
{
    public partial class Connetion_View : Form
    {

        // 建立一个连接
        public static ServerClient client = new ServerClient();
        public static ArrayList cmdlist = new ArrayList();
        public static string send_cmd="";                       // 发送数据
        public static string receive_cmd = "";                  // 接收数据
        public static string[] byte_string = new string[30];    // 30个数据

        int cmd_count = 0;
        public Connetion_View()
        {
            InitializeComponent();
            // Create_Cmd();
            // 创建30个label

            for (int i = 0; i < 30;i++)
            {
                //Label label = new Label();
                //label.Name = "showlabel" + i.ToString();
                //this.Controls.Add(label);
                //label.Left = (i % 6 + 1) * 100 + 500;
                //label.Top = (i / 6) * 30 + 100;
                //label.Text = i.ToString();
            }
                try
                {
                    IPAddress ip = IPAddress.Parse("192.168.0.192");
                    int port = 3000;
                    client.Connect(ip, port);
                }
                catch { MessageBox.Show("与PLC连接失败！"); }
        }

        private void Create_Cmd()
        {
            // 生成巡检命令
            //string cmd1 = "00 00 00 00 00 06 04 03 00 00 00 10";
            //string cmd2 = "00 00 00 00 00 06 04 03 00 10 00 10";
            //string cmd3 = "00 00 00 00 00 06 04 03 00 20 00 10";
            //string cmd4 = "00 00 00 00 00 06 04 03 00 30 00 10";
            //cmdlist.Clear();
            //cmdlist.Add(cmd1);
            //cmdlist.Add(cmd2);
            //cmdlist.Add(cmd3);
            //cmdlist.Add(cmd4);
        }

        private void Connetion_View_Load(object sender, EventArgs e)
        {
            // 连接至服务器
        }

        private void timer_show_Tick(object sender, EventArgs e)
        {
            // 将发送和接收的数据显示出来
            richTextBox_send.Text = send_cmd;
            richTextBox_receive.Text = receive_cmd;
            for(int i=0;i<30;i++)
            {
                foreach(Control mycontrol in Controls)
                {
                    if (mycontrol.Name == "textBox_data" + (i+1).ToString().PadLeft(2,'0'))
                    {
                        TextBox mylabel = (TextBox)mycontrol;
                        mylabel.Text = byte_string[i];
                        break;
                    }
                }
            }
        }

        private void timer_send_Tick(object sender, EventArgs e)
        {
            try
            {
                string send_cmd = "00 00 00 00 00 06 01 03 00 00 00 30";
                client.SendMessage(send_cmd);
            }
            catch
            {
                
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer_send.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer_send.Enabled = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            client.SendMessage(richTextBox1.Text);
        }
    }
}
